package com.eedi.framework.user.controller.org;

import com.eedi.framework.user.controller.org.vo.*;
import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.user.controller.org.vo.*;
import com.eedi.framework.user.service.OrgUserService;
import com.eedi.org.utils.HttpResponseUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import static com.eedi.framework.common.pojo.CommonResult.success;

@Tag(name = "经营界面 - 用户&账号")
@RestController
@RequestMapping("/org/user-account")
@Validated
public class OrgUserController {

    @Resource
    OrgUserService orgUserService;

    /**
     * 批量导入用户账号的excel模板文件
     */
    private final String BATCH_CREATE_USER_TEMPLATE_EXCEL = "用户账号批量导入模板.xlsx";


    /**
     * 批量导入用户-科室关系批量的excel模板文件
     */
    private final String BATCH_UPDATE_DEPT_TEMPLATE_EXCEL = "用户-科室关系批量导入模板.xlsx";

    @PostMapping("/create")
    @Operation(summary = "创建账号(org:user:create)")
    @PreAuthorize("@org.hasPermission('org:user:crate')")
    public CommonResult<OrgUserLoginInfoResp> create(@RequestBody OrgUserAccountCreateReq req) {
        return success(
                orgUserService.create(req)
        );
    }

    @PutMapping("/disable")
    @Operation(summary = "停用账号(org:user:update)")
    @Parameter(name = "orgUserId", description = "账号编号", required = true)
    @PreAuthorize("@org.hasPermission('org:user:update')")
    public CommonResult<Void> disable(@RequestParam("orgUserId") String orgUserId) {
        orgUserService.disableByOrgUserAccountId(orgUserId);
        return success(null);
    }

    @PutMapping("/enable")
    @Operation(summary = "启用账号(org:user:update)")
    @Parameter(name = "orgUserId", description = "账号编号", required = true)
    @PreAuthorize("@org.hasPermission('org:user:update')")
    public CommonResult<Void> enable(@RequestParam("orgUserId") String orgUserId) {
        orgUserService.enableByOrgUserAccountId(orgUserId);
        return success(null);
    }

    @GetMapping("/page")
    @Operation(summary = "账号列表(org:user:list)")
    @PreAuthorize("@org.hasPermission('org:user:list')")
    public CommonResult<PageResult<OrgUserLoginInfoResp>> list(OrgUserAccountPageReq req) {
        return success(orgUserService.page(req));
    }

    @PutMapping("/reset-password")
    @Operation(summary = "重置密码(org:user:update)")
    @Parameter(name = "orgUserId", description = "账号编号", required = true)
    @PreAuthorize("@org.hasPermission('org:user:update')")
    public CommonResult<String> resetPassword(@RequestParam String orgUserId) {
        String defautlPasswd = orgUserService.resetPassword(orgUserId);
        return success(defautlPasswd);
    }

    @PutMapping("/reset-mobile")
    @Operation(summary = "修改手机号(org:user:update)")
    @PreAuthorize("@org.hasPermission('org:user:update')")
    public CommonResult<Void> resetMobile(@RequestBody OrgUserAccountResetMobileReq req) {
        orgUserService.resetMobile(req);
        return success(null);
    }

    @PutMapping("/update")
    @Operation(summary = "修改用户信息(org:user:update)")
    @PreAuthorize("@org.hasPermission('org:user:update')")
    public CommonResult<Void> update(@RequestBody OrgUserUpReq req) {
        orgUserService.updateOrgUserInfo(req);
        return success(null);
    }

    @GetMapping("/count")
    @Operation(summary = "账号数量(org:user:list)")
    @PreAuthorize("@org.hasPermission('org:user:list')")
    public CommonResult<Long> count() {
        return success(orgUserService.countOrgUsers());
    }

    /**
     * @param fileUrl
     * @return
     * @throws Exception
     * @author: TomBombadil
     * @desc: 下载fileUrl对应的excel文件，解析文件数据后入库保存
     */
    @PostMapping("/batch-create")
    @Operation(summary = "批量添加用户(org:user:create)")
    @PreAuthorize("@org.hasPermission('org:user:create')")
    public CommonResult<OrgUserAccountBatchCreateResp> batchCreate(@RequestParam("fileUrl") String fileUrl) throws Exception {
        return success(orgUserService.batchCreate(fileUrl));
    }

    /**
     * 下载批量添加用户的excel模板文件
     *
     * @param response
     * @throws IOException
     */
    @GetMapping(value = "/excel-template")
    @Operation(summary = "下载批量导入用户的excel文件模板")
    public void excelTemplate(HttpServletResponse response) throws IOException {
        HttpResponseUtil.writeFile2Response(response, BATCH_CREATE_USER_TEMPLATE_EXCEL);
    }


    /**
     * 下载用户-科室关系批量导入模板
     *
     * @param response
     * @throws IOException
     */
    @GetMapping(value = "/user-dept-excel-template")
    @Operation(summary = "下载用户-科室关系批量导入模板")
    public void userDeptExcelTemplate(HttpServletResponse response) throws IOException {
        HttpResponseUtil.writeFile2Response(response, BATCH_UPDATE_DEPT_TEMPLATE_EXCEL);
    }


    /**
     * @param fileUrl
     * @return
     * @throws Exception
     * @author: TomBombadil
     * @desc: 批量更新用户-科室关系（Excel导入配置数据）
     */
    @PostMapping("/batch-update-user-dept")
    @Operation(summary = "批量维护用户-科室关系(org:user:update)")
    @PreAuthorize("@org.hasPermission('org:user:update')")
    public CommonResult<OrgUserDeptBatchUpdateResp> batchUpdateUserDept(@RequestParam("fileUrl") String fileUrl) throws Exception {
        return success(orgUserService.batchUpdateUserDept(fileUrl));
    }

    /**
     * 根据名称查询用户(左匹配)
     */
    @GetMapping("/find-user-simple")
    @Operation(summary = "根据名称查询用户(左匹配)")
//    @Parameter(name = "orgUserNameKey", description = "名称关键字")
//    @Parameter(name = "orgUserId", description = "机构用户ID")
    @PermitAll
    public CommonResult<List<OrgUserSimpleResp>> findUserSimple(OrgUserSimpleReq req) {

        return success(orgUserService.findUserSimple(req));
    }

}
